package lanou.a36ke_01.fragmengs.equityfragments;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import lanou.a36ke_01.activity.equityactivity.EquityDetailsActivity;
import lanou.a36ke_01.adapters.equityadapter.EquityAllAdapter;
import lanou.a36ke_01.beanlists.equitybean.EquityAllBean;
import lanou.a36ke_01.R;
import lanou.a36ke_01.tools.BaseFragment;
import lanou.a36ke_01.tools.MyApp;
import lanou.a36ke_01.activity.newsactivity.newstool.MySwipeRefreshLayout;
import lanou.a36ke_01.tools.NetTool;


public class EquityMzzFragment extends BaseFragment{

    private ListView listView;
    private EquityAllAdapter allAdapter;
    private String url = "https://rong.36kr.com/api/mobi/cf/actions/list?page=1&type=underway&pageSize=20";
    private MySwipeRefreshLayout mySwipeRefreshLayout;

    @Override
    protected int setLayout() {
        return R.layout.fragment_equitymzz;
    }

    @Override
    protected void initView() {
        listView = bindView(R.id.equitymzz_list_view);
        allAdapter = new EquityAllAdapter(MyApp.getContext());
        mySwipeRefreshLayout = bindView(R.id.equitymzz_fragment_my_srl);
        mySwipeRefreshLayout.setColorSchemeResources(android.R.color.holo_orange_light);

    }

    @Override
    protected void initData() {
        initJson();
        //下拉刷新
        mySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //清空ListView里面的全部数据
                listView.setAdapter(null);
                mySwipeRefreshLayout.setRefreshing(false);
                initJson();
            }
        });


    }
    protected void initJson(){
        NetTool netTool = new NetTool();
        netTool.getNetData(url, EquityAllBean.class, new NetTool.NetListener<EquityAllBean>() {
            @Override
            public void onSuccess(final EquityAllBean equityAllBean) {
                allAdapter.setEquityAllBean(equityAllBean);
                listView.setAdapter(allAdapter);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        Intent intent = new Intent(getContext(), EquityDetailsActivity.class);
                        intent.putExtra("ID", String.valueOf(equityAllBean.getData().getData().get(position).getCrowdFundingId()));
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }
}
